#!/usr/bin/bash
#
###############################################################################
# // From: https://openanolis.cn/sig/t-one/doc/1175048244250656802?lang=zh
# //        5.2.3 配置docker-compose.yaml替换变量
# //
# # 部署机外网IP地址(必填)
# server_ip=
#
# # 账号密码(必填)
# db_password=
# redis_password=
# tone_storage_password=
#
# # 镜像版本标签(非必改、默认latest)
# version_tag=latest
#
# # token(非必改、可自动生成)
# toneagent_access_key=$(uuidgen)
# toneagent_secret_key=$(uuidgen)
# admin_urls_token=$(uuidgen)
# pub_api_token=$(uuidgen) # 对外接口公共token
###############################################################################
#

FILE=$(readlink -f "${BASH_SOURCE[0]}")
CDIR=$(dirname "$FILE")
NAME=$(basename "$FILE")
TMPDIR=${TMPDIR:-"/tmp"}

function get_var_from_config
{
    typeset config_file=${1?"*** config file, e.g. mini-tone-config.yaml"}
    typeset var_name=${2?"*** var name, e.g. server_ip"}
    yamladm j $config_file | jq .${var_name} | xargs echo
}

function check_var
{
    typeset name=${1?"*** var name, e.g. server_ip"}
    typeset value=""
    eval "value=\$${name}"
    [[ $value == "null" ]] && value="" # XXX: YAML: null -> bash: ""
    [[ -z $value ]] && return 1 || return 0
}

mini_tone_config=${1?"$CDIR/../../config/mini-tone-config.yaml"}
src_docker_compose=${2?"$CDIR/../../config/x86_64/docker-compose_offline.yaml"}
dst_docker_compose=${3-"$CDIR/../../config/x86_64/docker-compose.yaml"}

db_password=$(get_var_from_config           $mini_tone_config "db_password")
redis_password=$(get_var_from_config        $mini_tone_config "redis_password")
tone_storage_password=$(get_var_from_config $mini_tone_config "tone_storage_password")
toneagent_access_key=$(get_var_from_config  $mini_tone_config "toneagent_access_key")
toneagent_secret_key=$(get_var_from_config  $mini_tone_config "toneagent_secret_key")
admin_urls_token=$(get_var_from_config      $mini_tone_config "admin_urls_token")
pub_api_token=$(get_var_from_config         $mini_tone_config "pub_api_token")
version_tag=$(get_var_from_config           $mini_tone_config "version_tag")
server_ip=$(get_var_from_config             $mini_tone_config "server_ip")

check_var "server_ip"
(( $? != 0 )) && echo "Oops, var 'server_ip' not set" >&2 && exit 1


cp $src_docker_compose $dst_docker_compose
sed -i "s/var_db_password/${db_password}/g"                      ${dst_docker_compose}
sed -i "s/var_redis_password/${redis_password}/g"                ${dst_docker_compose}
sed -i "s/var_tone_storage_password/${tone_storage_password}/g"  ${dst_docker_compose}
sed -i "s/var_toneagent_access_key/${toneagent_access_key}/g"    ${dst_docker_compose}
sed -i "s/var_toneagent_secret_key/${toneagent_secret_key}/g"    ${dst_docker_compose}
sed -i "s/var_admin_urls_token/${admin_urls_token}/g"            ${dst_docker_compose}
sed -i "s/var_pub_api_token/${pub_api_token}/g"                  ${dst_docker_compose}
sed -i "s/var_version_tag/${version_tag}/g"                      ${dst_docker_compose}
sed -i "s/var_server_ip/${server_ip}/g"                          ${dst_docker_compose}
